package com.sinaapp.kltq.dinosaurweather;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import com.sinaapp.kltq.weathermanager.WeatherManager;

import java.util.List;


public class MainActivity extends ActionBarActivity {

   /* @Override
    protected void onDestroy() {

        Intent localIntent = new Intent();
        localIntent.setClass(MainActivity.this, NoticeService.class); //销毁时重新启动Service
        System.out.println("重新启动");
        MainActivity.this.startService(localIntent);
        super onDestroy();

    }*/

    private Button btnRealtime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       /* if(!isServiceRunning(this,"NoticeService")) {


            System.out.println("isServiceRunning()");
            Intent intent = new Intent(MainActivity.this, NoticeService.class);
            //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            MainActivity.this.startService(intent);
        }
*/
        btnRealtime = (Button) findViewById(R.id.btnRealtime);

        btnRealtime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, BeginningActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
       /* if(!isServiceRunning(this,"NoticeService")) {

            Intent intent = new Intent(MainActivity.this, NoticeService.class);
            //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            MainActivity.this.startService(intent);

        }*/
    }

    //查看后台服务是否启动
    public static boolean isServiceRunning(Context mContext,String className) {
        boolean IsRunning = false;
        ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> serviceList   = activityManager.getRunningServices(30);
        if (!(serviceList.size()>0)) {
            return false;
        }
        for (int i=0; i<serviceList.size(); i++) {
            if (serviceList.get(i).service.getClassName().equals(className) == true) {
                IsRunning = true;
                break;
            }
        }
        return IsRunning ;
    }
}
